package fr.pentalog.ctc.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import ro.peoplecentric.ctc.common.dao.impl.BaseDaoImpl;
import ro.peoplecentric.ctc.common.entity.Item;
import fr.pentalog.ctc.dao.MyItemDao;

/**
 * @author Danut Chindris
 * @since September 22, 2013
 */

@Repository
public class MyItemDaoImpl extends BaseDaoImpl implements MyItemDao {

	private static final String DEFAULT_ORDER_BY = "id";
	private static final String DEFAULT_SORTING_ORDER = "asc";
	private static final String DEFAULT_SORTING_COLUMN = "title";

	@SuppressWarnings("unchecked")
	@Override
	public List<Item> getAllItems(String order, String orderBy) {
		final Criteria criteria = getSession().createCriteria(Item.class);
		String sortingOrder = DEFAULT_SORTING_ORDER;
		if (order != null && !order.isEmpty()) {
			sortingOrder = order;
		}
		String sortingColumn = DEFAULT_SORTING_COLUMN;
		if (orderBy != null && !orderBy.isEmpty()) {
			sortingColumn = orderBy;
		}
		if (sortingOrder.equals(DEFAULT_SORTING_ORDER)) {
			criteria.addOrder(Order.asc(sortingColumn));
		}
		else {
			criteria.addOrder(Order.desc(sortingColumn));
		}
        return criteria.list();
	}

	@Override
	protected Order getDefaultOrderBy() {
		return Order.asc(DEFAULT_ORDER_BY);
	}
}
